//
//  EeFilterDropView.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/5/6.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import Eelay
enum EeDropStatus{
    case showing
    case showed
    case dismissing
    case dismissed
}

public class EeFilterDropView:UIView {
    
    open var showMenuDuration = 0.4
    
    /// Hide menu second default value: *0.3*
    open var hideMenuDuration = 0.15
    
    /// Show menu spring velocity default value: *0.5*
    open var showMenuSpringVelocity:CGFloat = 0.5
    
    /// Show menu spring damping default value: *0.8*
    open var showMenuSpringWithDamping:CGFloat = 0.8
    
    /// Hide menu spring velocity Default value: *0.9*
    open var hideMenuSpringVelocity:CGFloat = 0.9
    
    /// Hide menu spring damping Default value: *0.8*
    open var hideMenuSpringWithDamping:CGFloat = 0.8
    
    /// Bottom 0.5px line
    open var bottomLine: UIView!
    
    weak var menu:EeFilterMenu? = nil
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.eelay = [
            [touchBK,[ee.T.L.B.R]],
        ]
        touchBK.addTarget(self, action: #selector(close), for: .touchUpInside)
        
    }
    
    var cons = [NSLayoutConstraint]()
    
    var sb_cons = [NSLayoutConstraint]()
    
    var top_constrain = NSLayoutConstraint()
    
    var touchBK = UIControl()
    
    
    var status:EeDropStatus = .dismissed
    {
        didSet{
            switch status {
            case .dismissed,.showed:
                self.menu?.isUserInteractionEnabled = true
            default:
                self.menu?.isUserInteractionEnabled = false
            }
        }
    }
    
    
    init(with menu:EeFilterMenu?) {
        super.init(frame: .zero)
        self.menu = menu
        self.eelay = [
            [touchBK,[ee.T.L.B.R]],
        ]
        touchBK.addTarget(self, action: #selector(close), for: .touchUpInside)
    }
    
    
    
    
    
    
    public var currentItem:FilterOption? = nil
    
    public weak var container:UIView?
    
    open var backView:UIView?{
        get{
            if let v = container{
                return v
            }
            return UIApplication.shared.keyWindow
        }
    }
    
    public func show(item:FilterOption) {
        switch status{
        case .dismissing,.showed,.showing:
            return
        default:
            break
        }
        
        self.currentItem = item
        self.clipsToBounds = true
        if let v = menu
        {
            switch item.filterSelector{
            case .dropView(let selector):
                let dropDownView = selector
                
                if let sview = backView
                {
                    if self.superview == nil
                    {
                        let lays:TP.lays = [
                            [self,[ee.L.B.R,[0.+1000,0.+1000,0.+1000]],[v,ee.B,ee.T,0.+1000]]
                        ]
                        
                        self.cons = sview.setEeLays(lays: lays).1
                        touchBK.backgroundColor = UIColor(white: 0.2, alpha: 0.3)
                        self.backgroundColor = UIColor.clear
                        touchBK.alpha = 0
                    }
                    self.status = .showing
                    
                    let menu_height = dropDownView.flexBoundsFix
                    let height = dropDownView.intrinsicContentSize.height
                    self.addSubview(dropDownView)
                    dropDownView.frame = [0,-menu_height,v.frame.width,height]
                    let oldCenter = dropDownView.center
                    dropDownView.center = [oldCenter.x,(oldCenter.y-height)]
                    
                    
                    UIView.animate(
                        withDuration: self.showMenuDuration,
                        delay: 0,
                        usingSpringWithDamping: self.showMenuSpringWithDamping,
                        initialSpringVelocity: self.showMenuSpringVelocity,
                        options: [],
                        animations: {
                            dropDownView.center = oldCenter
                            self.touchBK.alpha = 1
                    }, completion: { _ in
                        dropDownView.center = oldCenter
                        self.status = .showed
                    })
                }
            default:
                break
                
            }
            
            
        }
    }
    
    func open(item:FilterOption) {
        item.isSelected = true
        switch self.status {
        case .showed,.showing:
            if let old_item = self.currentItem{
                if old_item === item
                {
                    return
                }
                else{
                    self.dissmis(item: old_item, commplete: {
                        self.show(item: item)
                    })
                }
            }
        default:
            show(item: item)
        }
        
    }
    
    @objc public func close() {
        if status == .dismissed{
            return
        }
        if let item = self.currentItem
        {
            self.dissmis(item: item, commplete: {})
        }
        self.status = .dismissing

        self.currentItem?.isSelected = false
    
        UIView.animate(
            withDuration: self.hideMenuDuration,
            delay: 0,
            usingSpringWithDamping: self.hideMenuSpringWithDamping,
            initialSpringVelocity: self.hideMenuSpringVelocity,
            options: [],
            animations: {
                self.touchBK.alpha = 0
        }, completion: { _ in
            self.status = .dismissed
            self.currentItem = nil
            self.removeFromSuperview()
            self.superview?.removeConstraints(self.cons)
        })
        
    }
    
    func dissmis(item:FilterOption,commplete:@escaping ()->Void) {
        switch status{
        case .showing,.dismissing,.dismissed:
            return
        default:
            break
        }
        self.status = .dismissing
        item.isSelected = false
        
        switch item.filterSelector {
        case .dropView(let selector):
            let dropDownView = selector
            let height = dropDownView.intrinsicContentSize.height
            let oldCenter = dropDownView.center
            let newCenter:CGPoint = [oldCenter.x,(oldCenter.y-height)]
            UIView.animate(
                withDuration: self.hideMenuDuration,
                delay: 0,
                usingSpringWithDamping: self.hideMenuSpringWithDamping,
                initialSpringVelocity: self.hideMenuSpringVelocity,
                options: [],
                animations: {
                    dropDownView.center = newCenter
            }, completion: { _ in
                self.status = .dismissed
                self.currentItem = nil
                commplete()
            })
        default:
            break
        }
        
        
        
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var contentView:UIView?{
        get{
            return nil
        }
    }
}

